<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    interval: {
      type: Number,
      default: 0,
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '1800px'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    datetimes: {
      type: Array,
      required: true
    },
    powerMax: {
      type: Object,
      required: true
    },
    powerUnit: {
      type: Array,
      required: true
    },
    powerColor: {
      type: String
    },
    powerType: {
      type: String
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData, dataV, dataA } = {}) {
      if(this.powerType == '功率') {
        this.chart.setOption({
          xAxis: {
            data: this.datetimes,
            boundaryGap: false,
            axisTick: {
              show: false
            },
            nameTextStyle: {fontStyle: 'oblique'},
            axisLabel: {
              showMaxLabel: true,
              interval: this.interval,
              rotate: 20//角度顺时针计算的
            }
          },
          grid: {
            left: 10,
            right: '10%',
            bottom: 20,
            top: 30,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10],
            /*formatter: '{b0}<br />' + this.powerType + '：{c0}<br />{c1}<br />{c2}',*/
          },
          yAxis: [
            {
              type: 'value',
              name: expectedData.length > 0 ? '功率' : '',
              axisTick: {
                show: false
              },
              //max: this.powerMax.maxW,
              axisLine: {
                show: expectedData.length > 0 ? true : false,
                lineStyle: {
                }
              },
              axisLabel: {
                formatter: '{value}'+ this.powerUnit[0]
              },
            },
            {
              type: 'value',
              offset: 0,
              name: dataV.length > 0 ? '电压' : '',
              position: 'right',
              alignTicks: {
                show: false
              },
              axisLine: {
                show: dataV.length > 0 ? true : false,
                lineStyle: {
                }
              },
              //max: this.powerMax.maxV,
              axisLabel: {
                formatter: '{value}' + this.powerUnit[1]
              }
            },
            {
              type: 'value',
              offset: 75,
              name: dataA.length > 0 ? '电流' : '',
              position: 'right',
              alignTicks: {
                show: false
              },
              //max: this.powerMax.maxV,
              axisLine: {
                show: dataA.length > 0 ? true : false,
                lineStyle: {
                }
              },
              axisLabel: {
                formatter: '{value}' +  this.powerUnit[2]
              }
            }
          ],
          legend: {
            data: ['功率', '电压', '电流' ],
            show: true
          },
          series: [
            {
              name: '功率', itemStyle: {
                normal: {
                  color: this.powerColor,
                  lineStyle: {
                    color: this.powerColor,
                    width: 2
                  }
                }
              },
              colorBy: 'series',
              smooth: true,
              type: 'line',
              symbol: 'circle',
              symbolSize: 5,
              label: {
                show: false,
                position: 'left'
              },
              data: expectedData,
              animationDuration: 2800,
              animationEasing: 'cubicInOut'
            },
            {
              name: '电压',
              type: 'line',
              symbol: 'circle',
              symbolSize: 5,
              yAxisIndex: 1,
              data: dataV || [],
              animationDuration: 2800,
              animationEasing: 'cubicInOut'
            },
            {
              name: '电流',
              type: 'line',
              symbol: 'circle',
              symbolSize: 5,
              yAxisIndex: 2,
              data: dataA || [],
              animationDuration: 2800,
              animationEasing: 'cubicInOut'
            },
          ]
        }, true)
      }else if(this.powerType == 'SOC') {
        this.chart.setOption({
          xAxis: {
            data: this.datetimes,
            boundaryGap: false,
            axisTick: {
              show: false
            },
            nameTextStyle: {fontStyle: 'oblique'},
            axisLabel: {
              showMaxLabel: true,
              interval: this.interval,
              rotate: 20//角度顺时针计算的
            }
          },
          grid: {
            left: 10,
            right: '10%',
            bottom: 20,
            top: 30,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10],
            /*formatter: '{b0}<br />' + this.powerType + '：{c0}<br />{c1}<br />{c2}',*/
          },
          yAxis: [
            {
              type: 'value',
              name: expectedData.length > 0 ? 'SOC' : '',
              axisTick: {
                show: false
              },
              //max: this.powerMax.maxW,
              axisLine: {
                show: expectedData.length > 0 ? true : false,
                lineStyle: {
                }
              },
              axisLabel: {
                formatter: '{value}'+ this.powerUnit[0]
              },
            },
            {
              type: 'value',
              offset: 0,
              name: dataV.length > 0 ? '订单已充电量' : '',
              position: 'right',
              alignTicks: {
                show: false
              },
              axisLine: {
                show: dataV.length > 0 ? true : false,
                lineStyle: {
                }
              },
              //max: this.powerMax.maxV,
              axisLabel: {
                formatter: '{value}' + this.powerUnit[1]
              }
            },
          ],
          legend: {
            data: ['SOC', '订单已充电量'],
            show: true
          },
          series: [
            {
              name: 'SOC', itemStyle: {
                normal: {
                  color: this.powerColor,
                  lineStyle: {
                    color: this.powerColor,
                    width: 2
                  }
                }
              },
              colorBy: 'series',
              smooth: true,
              type: 'line',
              symbol: 'circle',
              symbolSize: 5,
              label: {
                show: false,
                position: 'left'
              },
              data: expectedData,
              animationDuration: 2800,
              animationEasing: 'cubicInOut'
            },
            {
              name: '订单已充电量',
              type: 'line',
              symbol: 'circle',
              symbolSize: 5,
              yAxisIndex: 1,
              data: dataV || [],
              animationDuration: 2800,
              animationEasing: 'cubicInOut'
            }

          ]
        }, true)
      }else if (this.powerType == '温度') {
        if(expectedData.length > 0 && dataV.length > 0 && dataA.length > 0) {
          this.chart.setOption({
            xAxis: {
              data: this.datetimes,
              boundaryGap: false,
              axisTick: {
                show: false
              },
              nameTextStyle: {fontStyle: 'oblique'},
              axisLabel: {
                showMaxLabel: true,
                interval: this.interval,
                rotate: 20//角度顺时针计算的
              }
            },
            grid: {
              left: 10,
              right: '10%',
              bottom: 20,
              top: 30,
              containLabel: true
            },
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'cross'
              },
              padding: [5, 10],
              /*formatter: '{b0}<br />' + this.powerType + '：{c0}<br />{c1}<br />{c2}',*/
            },
            yAxis: [
              {
                type: 'value',
                name: expectedData.length > 0 ? '桩温' : '',
                axisTick: {
                  show: false
                },
                //max: this.powerMax.maxW,
                axisLine: {
                  show: expectedData.length > 0 ? true : false,
                  lineStyle: {
                  }
                },
                axisLabel: {
                  formatter: '{value}'+ this.powerUnit[0]
                },
              },
              {
                type: 'value',
                offset: 0,
                name: dataV.length > 0 ? '枪温+' : '',
                position: 'right',
                alignTicks: {
                  show: false
                },
                axisLine: {
                  show: dataV.length > 0 ? true : false,
                  lineStyle: {
                  }
                },
                //max: this.powerMax.maxV,
                axisLabel: {
                  formatter: '{value}' + this.powerUnit[1]
                }
              },
              {
                type: 'value',
                offset: 75,
                name: dataA.length > 0 ? '枪温-' : '',
                position: 'right',
                alignTicks: {
                  show: false
                },
                //max: this.powerMax.maxV,
                axisLine: {
                  show: dataA.length > 0 ? true : false,
                  lineStyle: {
                  }
                },
                axisLabel: {
                  formatter: '{value}' +  this.powerUnit[2]
                }
              }
            ],
            legend: {
              data: ['桩温', '枪温+', '枪温-'],
              show: true
            },
            series: [
              {
                name: '桩温',
                itemStyle: {
                  normal: {
                    color: this.powerColor,
                    lineStyle: {
                      color: this.powerColor,
                      width: 2
                    }
                  }
                },
                colorBy: 'series',
                smooth: true,
                type: 'line',
                symbol: 'circle',
                symbolSize: 5,
                label: {
                  show: false,
                  position: 'left'
                },
                data: expectedData || [],
                animationDuration: 2800,
                animationEasing: 'cubicInOut'
              },
              {
                name: '枪温+',
                type: 'line',
                symbol: 'circle',
                symbolSize: 5,
                yAxisIndex: 1,
                data: dataV || [],
                animationDuration: 2800,
                animationEasing: 'cubicInOut'
              },
              {
                name: '枪温-',
                type: 'line',
                symbol: 'circle',
                symbolSize: 5,
                yAxisIndex: 2,
                data: dataA || [],
                animationDuration: 2800,
                animationEasing: 'cubicInOut'
              }
            ]
          }, true)
        }else{
          this.chart.setOption({
            xAxis: {
              data: this.datetimes,
              boundaryGap: false,
              axisTick: {
                show: false
              },
              nameTextStyle: {fontStyle: 'oblique'},
              axisLabel: {
                showMaxLabel: true,
                interval: this.interval,
                rotate: 20//角度顺时针计算的
              }
            },
            grid: {
              left: 10,
              right: '10%',
              bottom: 20,
              top: 30,
              containLabel: true
            },
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'cross'
              },
              padding: [5, 10],
              /*formatter: '{b0}<br />' + this.powerType + '：{c0}<br />{c1}<br />{c2}',*/
            },
            yAxis: [
              {
                type: 'value',
                name: '枪温',
                axisTick: {
                  show: false
                },
                //max: this.powerMax.maxW,
                axisLine: {
                  show: expectedData.length > 0 ? true : false,
                  lineStyle: {
                  }
                },
                axisLabel: {
                  formatter: '{value}'+ this.powerUnit[0]
                },
              }

            ],
            legend: {
              data: ['枪温'],
              show: true
            },
            series: [
              {
                name: '枪温',
                itemStyle: {
                  normal: {
                    color: this.powerColor,
                    lineStyle: {
                      color: this.powerColor,
                      width: 2
                    }
                  }
                },
                colorBy: 'series',
                smooth: true,
                type: 'line',
                symbol: 'circle',
                symbolSize: 5,
                label: {
                  show: false,
                  position: 'left'
                },
                data: expectedData || [],
                animationDuration: 2800,
                animationEasing: 'cubicInOut'
              }
            ]
          }, true)
        }
      }

    }
  }
}
</script>
